package com.compass.doctor.ui.patient.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.databinding.FragmentBaseListBinding
import com.compass.doctor.ui.patient.adapter.CaseHistoryAdapter
import com.compass.doctor.ui.patient.viewmodel.PatientViewModel
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.ui.patient.AddInquiryCaseHistory
import com.compass.framework.constant.KEY_PATIENT_ID
import com.compass.framework.constant.REFRESH_CASE
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.visible
import com.compass.framework.utils.LiveDataBus
import com.compass.framework.utils.dpToPx
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 病历记录
 * Date:2023/12/8 11:20
 */
class CaseHistoryFragment : BaseVBFragment<FragmentBaseListBinding>(), OnRefreshListener,
    OnLoadMoreListener {

    private lateinit var mAdapter: CaseHistoryAdapter
    private val patientViewModel: PatientViewModel by viewModels()
    private var mPage = 1
    private lateinit var patientId: String//患者id
    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            patientId = it.getString(KEY_PATIENT_ID)!!
        }
        mBinding?.refreshLayout?.apply {
            setEnableRefresh(true)
            setEnableLoadMore(false)
            setOnRefreshListener(this@CaseHistoryFragment)
            setOnLoadMoreListener(this@CaseHistoryFragment)
        }

        mAdapter = CaseHistoryAdapter()
        mBinding?.recyclerView?.apply {
            layoutManager =
                LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
            adapter = mAdapter
            addItemDecoration(NormalItemDecoration().apply {
                setBounds(
                    left = dpToPx(8),
                    top = dpToPx(10),
                    right = dpToPx(8),
                    bottom = dpToPx(10)
                )
                setLastBottom(true)
            })
        }
    }

    override fun intListener() {
        mAdapter.onItemClickListener = { view, position ->
            val item = mAdapter.getItem(position)
            item?.let {
                AddInquiryCaseHistory.start(requireContext(), patientId,null,item.id,null,0)

//                CaseHistoryDetailsActivity.start(requireContext(),it)
            }

        }
    }

    override fun initData() {
        getCaseHistory()
        LiveDataBus.get().with(REFRESH_CASE, String::class.java).observe(this) {
            getCaseHistory()
        }
        patientViewModel.caseHistoryLiveData.observe(this) {
            dismissLoading()
            if (mPage == 1) {
                mAdapter.setData(it)
                mBinding?.refreshLayout?.finishRefresh()
//                mBinding?.refreshLayout?.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding?.emptyView?.visible()
                } else {
                    mBinding?.emptyView?.gone()
                }
            } else {
                mBinding?.refreshLayout?.finishLoadMore()
                if (!it.isNullOrEmpty()) {
                    mAdapter.addAll(it)
                } else {
                    mBinding?.refreshLayout?.setEnableLoadMore(false)
                }
            }
        }

    }

    private fun getCaseHistory() {
        patientViewModel.getPatientCaseHistory(mPage, patientId)
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage == 1
        getCaseHistory()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getCaseHistory()
    }
}